<?
/**
 * VoiceOne Settings/User/edit
 *
 * Frontend to provide access to the tech system in voiceone.
 * @package voiceone
 * @subpackage  settings_user
 */
 
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
 	sajax_init();
  
  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    $data=$_POST["user"];
    //define user structure
    $entry=Array(
            "username" => $data["username"],
            "lang" => $data["language"],
            "type" => $data["type"],
            "entity_id" => $data["entity_id"]
          ); 
    if($data["id"]!="") $entry["id"]=$data["id"]; //modify mode or insert mode?
    $pwd_error="";
    if((isset($data["changepwd"]))&&($data["changepwd"]=="on")){ //check password
      if($data["newpassword"]!=""){
        if($data["newpassword"]==$data["confirmpassword"]){
          $entry["password"] = $data["newpassword"];
        }else $pwd_error=$cwlang["user"]["err1"];
      }else $pwd_error=$cwlang["user"]["err2"];
    }else $entry["password"]=$data["password"]; 
    //create/update user
    if($pwd_error=="") {
      $ret=$asterisk->putUser($entry);
      if ($ret>0) $alert="<div class=\"AlertGood\">".$cwlang["user"]["alertModGood"]."</div>";
      else $alert="<div class=\"AlertBad\">".$cwlang["user"]["alertModBad"]."</div>";
    }else $alert="<div class=\"AlertBad\">".$pwd_error."</div>";
    goToMainPage($alert);
  }
  
  //get the client informations
  if (isset($_GET["id"]) and $_GET["id"]!="") {
    $id=$_GET["id"];
    $ret=$asterisk->getUser($id);
    if ($err=$asterisk->getError()) die("Error: $err");
    $v=$ret[0];
    $id=$v["id"];
    $username=$v["username"];
    $password=$v["password"];
    $lang=$v["lang"];
    $type=$v["type"];
    $entity_id=$v["entity_id"];
    //check if it is locked
    if($entity_id>0) $disabled=" disabled=\"disabled\" "; else $disabled="";
    $title=$cwlang["user"]["modUser"]." ".$username;
  }else $title=$cwlang["user"]["newUser"];
  //create the language list
  $availLang=getAvailLang();
  foreach($availLang as $l) {
    if ($l==$lang) $s="selected=\"selected\""; else $s="";
    $select.="<option value=\"$l\" $s>$l</option>\n";
  }
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
  <?=$alert?>
    <h1><?=$title?></h1>
    <h3><a href="../" class="admin" ><?=$cwlang["user"]["return"]?></a> </h3>
  </div>

  <div id="basic_form">
    <form action="<?=$PHP_SELF?><?if ($_GET["id"]!="") echo "?id=".$_GET["id"]; ?>" method="post">
      <!-- User info -->
      <div id="client_form" >
        <h3><?=$cwlang["user"]["title"]?></h3>
        <p class="blockintro"><?=$cwlang["user"]["desc"]?></p>
        <div class="block">
          <dl>
            <dt class="required"><label for="user[username]"><?=$cwlang["user"]["username"]?>:</label></dt>
              <dd><input type="text" size="25" maxlength="30" id="user[username]" name="user[username]" value="<?=$username?>" <?=$disabled?>/></dd>
          <? if ($_GET["id"]=="") { ?>  
            <dt class="required"><label for="user[password]"><?=$cwlang["user"]["password"]?>:</label></dt>
              <dd><input type="password" size="25" maxlength="30" id="user[password]" name="user[password]" value="<?=$password?>" /></dd>
            <dt class="required"><label for="user[language]"><?=$cwlang["user"]["language"]?>:</label></dt>
                <dd><select id="user[language]" name="user[language]" ><?=$select?></select></dd>
          <? } else { ?>
            <dt class="required"><label for="user[password]"><?=$cwlang["user"]["password"]?>:</label></dt>
              <dd><input type="password" size="25" maxlength="30" id="password" name="password" value="<?=$password?>" disabled="disabled" /></dd>     
            <? if ($entity_id == 0) { ?>
              <dt class="required"><label for="user[changepwd]"></label></dt>
                <dd><input type="checkbox" id="user[changepwd]" name="user[changepwd]" onclick="if(this.checked){document.getElementById('user[newpassword]').disabled=''; document.getElementById('user[confirmpassword]').disabled=''; } else {document.getElementById('user[newpassword]').disabled='disabled'; document.getElementById('user[confirmpassword]').disabled='disabled'; document.getElementById('user[newpassword]').value=''; document.getElementById('user[confirmpassword]').value=''; }"/>
                    <?=$cwlang["user"]["changepwd"]?>
                    <input type="hidden" id="user[password]" name="user[password]" value="<?=$password?>" /></dd>         
              <dt class="required"><label for="user[newpassword]"><?=$cwlang["user"]["newpassword"]?>:</label></dt>
                <dd><input type="password" size="25" maxlength="20" id="user[newpassword]" name="user[newpassword]" value="" disabled="disabled" /></dd>
              <dt class="required"><label for="user[confirmpassword]"><?=$cwlang["user"]["confirmpassword"]?>:</label></dt>
                <dd><input type="password" size="25" maxlength="20" id="user[confirmpassword]" name="user[confirmpassword]" value="" disabled="disabled" /></dd>
              <dt class="required"><label for="user[language]"><?=$cwlang["user"]["language"]?>:</label></dt>
                <dd><select id="user[language]" name="user[language]" ><?=$select?></select></dd>
            <? } else { ?>
              <dt class="required"><label for="user[language]"><?=$cwlang["user"]["language"]?>:</label></dt>
                <dd><select id="language" name="language" disabled="disabled" ><?=$select?></select>
                    <input type="hidden" id="user[language]" name="user[language]" value="<?=$lang?>" /></dd>
            <? } ?>   
          <? } ?>  
          </dl>
        </div>
        <!-- User type -->
        <h3><?=$cwlang["user"]["typeTitle"]?></h3>
        <p class="blockintro"><?=$cwlang["user"]["typeDesc"]?></p>
          <dl>
            <dt class="required"><label for="user[type]"><?=$cwlang["user"]["type"]?>:</label></dt>
            <dd>
              <img src="<?=$relBasePath?>/public/img/user_standard.gif" alt="Standard User" />
              <input type="radio" size="25" maxlength="30" id="user[type1]" name="user[type]" value="<?=USER_STANDARD?>" <?if ($type==USER_STANDARD) echo "checked=\"checked\"";?> />
              <?=$cwlang["user"]["standard"]?>
              <img src="<?=$relBasePath?>/public/img/user_configurator.gif" alt="Configurator User" />
              <input type="radio" size="25" maxlength="30" id="user[type2]" name="user[type]" value="<?=USER_CONFIGURATOR?>" <?if ($type==USER_CONFIGURATOR) echo "checked=\"checked\"";?> />
              <?=$cwlang["user"]["configurator"]?>
          <?  if ($cwUserInfo["type"]==USER_SUPERUSER ) { ?>              
              <img src="<?=$relBasePath?>/public/img/user_superuser.gif" alt="Superuser" />
              <input type="radio" size="25" maxlength="30" id="user[type3]" name="user[type]" value="<?=USER_SUPERUSER?>" <?if ($type==USER_SUPERUSER) echo "checked=\"checked\"";?>/>
              <?=$cwlang["user"]["superuser"]?>
          <?  } ?>
            </dd>
          </dl>
      </div>      
    <div class="action">
      <?if ($_GET["id"]!="") {?> <input type="hidden" name="user[id]" value="<?=$id?>" /><?}?>
      <input type="hidden" name="user[entity_id]" value="<?=$entity_id?>" />
      <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;"/>
      <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
    </div>
  </form>
 </div>  
  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["user"]["sidebarTitle"]?></h1>
          <?=$cwlang["user"]["sidebarDesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
 	sajax_handle_client_request();

  $smarty->assign("section", "user");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->assign("ajax", $js);
  $smarty->display('settings.tpl');
?>

